/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreLocation;
using MonoTouch.MapKit;

namespace ArtNotes
{
	/// <summary>
	/// This shows the map view and generates the 'pins'
	/// showing where a photograph was taken. Although it
	/// works with a small amount of images, it is a
	/// concern that 100's of images may cause a slow-down.
	/// </summary>
	public class ANMapViewTabController : UIViewController
	{

		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage, string title)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.SetTitle (title, UIControlState.Normal);
			button.Font = UIFont.FromName("Marker Felt",18);
			button.SetTitleColor (UIColor.White, UIControlState.Normal);
			
			button.Tag = 1;
			return button;
		}
		
		// From the monotouch samples
		private UIButton RoundedButtonType (float xpos, float ypos, string btitle)
		{
			var button = UIButton.FromType (UIButtonType.RoundedRect);
			button.Frame = new RectangleF (xpos, ypos, 106f, 40f);
			button.BackgroundColor = UIColor.Clear;
			button.SetTitle (btitle, UIControlState.Normal);
			button.SetTitleColor (UIColor.Black, UIControlState.Normal);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		private CLLocationManager locationManager;
		private UIButton btnMap;
		private UIButton btnSatalite;
		private UIButton btnHybrid;
		private MKMapView mapView;
		//MKReverseGeocoder geoCoder;
		private UIImageView imgOverlay;
		
		private ANDatabase anDB;
		
		public ANMapViewTabController ()
		{
		}
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			anDB = AN_APP_OPTIONS.AN_DATABASE;
			
			//btnMap = RoundedButtonType (0, 300, "Map");
			btnMap = ImageButtonType(new RectangleF(20,320,100,27),UIImage.FromFile("Images/map_view_buttons.png"),UIImage.FromFile("Images/map_view_buttons.png"), "Map");
			//btnSatalite = RoundedButtonType (107, 300, "Satalite");
			btnSatalite = ImageButtonType(new RectangleF(120,320,100,27),UIImage.FromFile("Images/map_view_buttons.png"),UIImage.FromFile("Images/map_view_buttons.png"), "Satellite");
			//btnHybrid = RoundedButtonType (214, 300, "Hybrid");
			btnHybrid = ImageButtonType(new RectangleF(220,320,100,27),UIImage.FromFile("Images/map_view_buttons.png"),UIImage.FromFile("Images/map_view_buttons.png"), "Hybrid");
			
			imgOverlay = new UIImageView (new RectangleF (0, 0, 320, 480));
			imgOverlay.Image = UIImage.FromFile("Images/map_tab_bg.png");
			
			//locationManager = new CLLocationManager ();
			//locationManager.StartUpdatingLocation ();
			locationManager = AN_APP_OPTIONS.AN_LOCATION;
			
			mapView = new MKMapView ();
			mapView.Frame = new RectangleF (0, 0, 320, 370);
			
			mapView.MapType = MKMapType.Standard;
			
//			geoCoder = new MKReverseGeocoder ();
			
			btnMap.TouchDown += delegate {
				//CLLocationManager clm = AN_APP_OPTIONS.AN_LOCATION;
				//if (clm.Location != null)
				//{
				//	Console.WriteLine ("clm Latitude is: " + clm.Location.Coordinate.Latitude.ToString ());
				//	Console.WriteLine ("clm Longitude is: " + clm.Location.Coordinate.Longitude.ToString ());
				//}
				this.mapView.MapType = MKMapType.Standard;
			};
			btnSatalite.TouchDown += delegate {
				this.mapView.MapType = MKMapType.Satellite;	
			};
			btnHybrid.TouchDown += delegate {
				this.mapView.MapType = MKMapType.Hybrid;
			};
			//this.View.AddSubview (button);
			this.View.AddSubview (mapView);
			this.View.AddSubview (btnMap);
			this.View.AddSubview (btnSatalite);
			this.View.AddSubview (btnHybrid);
			this.View.AddSubview (imgOverlay);
		}
		
		public override void ViewDidAppear (bool animated)
		{
			base.ViewDidAppear (animated);
			
			CLLocationCoordinate2D locationCoordinate;
			
			if(locationManager.Location != null)
			{
				locationCoordinate = new CLLocationCoordinate2D (locationManager.Location.Coordinate.Latitude, locationManager.Location.Coordinate.Longitude);
				mapView.SetCenterCoordinate (
					locationCoordinate,
					true
				);
			}
			else
			{
				locationCoordinate = new CLLocationCoordinate2D (53.573725, -2.436157);
				mapView.SetCenterCoordinate (
					locationCoordinate,
					true
				);
			}
			
			//ANAnnotation a = new ANAnnotation (
			//new CLLocationCoordinate2D (53.573725, -2.436157),
			//	"Work",
			//	"University of Bolton"
			//);
			
			MKCoordinateSpan span = new MKCoordinateSpan (0.02, 0.02);
			
			MKCoordinateRegion region = new MKCoordinateRegion (locationCoordinate, span);
			
			mapView.SetRegion(region, true);
			
			Image[] images = anDB.ListAllImages().ToArray();
			ANAnnotation newAnnotation;
			CLLocationCoordinate2D newAnnotationCoordinate;

			for(int i = 0; i < images.Length; i++)
			{
				// Although you can be at 0,0, given that it's in the middle of the sea
				// it's unlikely anyone will take a picture there. On the off chance that
				// they do, it won't be displayed.
				if(!((images[i].Lattitude == 0) && (images[i].Longitude == 0)))
				{
					newAnnotationCoordinate = new CLLocationCoordinate2D(images[i].Lattitude,images[i].Longitude);
					newAnnotation = new ANAnnotation(newAnnotationCoordinate,images[i].Title,images[i].Description);
            			mapView.AddAnnotationObject(newAnnotation);
				}
			}
		}


	}
	
	// Taken from http://conceptdev.blogspot.com
	public class ANAnnotation : MKAnnotation
        {
            private CLLocationCoordinate2D _coordinate;
            private string _title, _subtitle;

            public override CLLocationCoordinate2D Coordinate {
                get {
                    return _coordinate;
                }
            }

            public override string Title {
                get {
                    return _title;
                }
            }

            public override string Subtitle {
                get {
                    return _subtitle;
                }
            }

            /// <summary>
            /// custom constructor
            /// </summary>
            public ANAnnotation (CLLocationCoordinate2D coord, string title, string subtitle) : base()
            {
                _coordinate=coord;
                 _title=title; 

                _subtitle=subtitle;
            }
        }
}
